var App = getApp();

import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';

//Page Object
Page({
    data: {
        page: {
            page: 1,
            pageSize: 10
        },
        imageList: [],
        selectCount: 0,
        isNoMore: false
    },
    //options(Object)
    onLoad: function (options) {
        this.getData()
    },

    onShow: function () {

    },
    onHide: function () {

    },

    onChange: function (event) {
        var index = event.currentTarget.dataset.index
        this.data.imageList[index].selected = !this.data.imageList[index].selected

        var selectArr = this.data.imageList.filter((item) => {
            return item.selected == true
        })
        this.setData({
            imageList: this.data.imageList,
            selectCount: selectArr.length
        })
    },

    getData: function () {
        const _this = this
        let params = Object.assign({}, _this.data.page)
        App.globalData.userModel.getImageList({
            data: params
        }).then(json => {
            if (_this.data.page.page == 1) {
                _this.setData({
                    imageList: json.data.list,
                    'page.page': ++_this.data.page.page
                })
            } else {
                _this.setData({
                    'page.page': ++_this.data.page.page,
                    imageList: _this.data.imageList.concat(json.data.list)
                })
            }
            var selectArr = _this.data.imageList.filter((item) => {
                return item.selected == true
            })
            _this.setData({
                'selectCount': selectArr.length
            })
            if (json.data.list.length < _this.data.page.pageSize) {
                _this.setData({
                    isNoMore: true
                })
            }
        })
    },
    onSubmit: function () {
        const _this = this
        var ids = new Array()
        for (let index = 0; index < _this.data.imageList.length; index++) {
            const element = _this.data.imageList[index];
            if (element.selected == true) {
                ids.push(element.id)
            }
        }
        App.globalData.userModel.delPic({
            data: {
                ids: JSON.stringify(ids)
            }
        }).then(json => {
            Toast(json.data)
            var updateImgList = _this.data.imageList.filter((item=>{
                return ids.indexOf(item.id) < 0
            }))
            _this.setData({
                imageList:updateImgList
            })
        })
    },
    onReachBottom: function () {
        if (!this.data.isNoMore)
            this.getData()
    },

});